package edu.rmit.client;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import edu.rmit.domain.User;
import edu.rmit.service.IGroupService;
import edu.rmit.util.PromotDialog;

/**
 * This class is to display the create group panel. And also deal with when some
 * related button is clicked.
 * 
 * @author Joey George
 * @version 1.0
 */
public class CreateGroupPanel {

	/**
	 * The JFrame which display the system window.
	 */
	private JFrame frame;

	/**
	 * The window initialize height.
	 */
	private static int HEIGHT = 300;  

	/**
	 * The window initialize height.
	 */
	private static int WIDTH = 400;

	/**
	 * The label which display the group name.
	 */
	private JLabel groupnameLabel;

	/**
	 * The label which display the group description.
	 */
	private JLabel descriptionLabel;

	/**
	 * The textField which accept the group name.
	 */
	private JTextField groupnameField;

	/**
	 * The textField which accept the group description.
	 */
	private JTextArea descriptionTextArea;

	/**
	 * The button which clicked will submit the create group request.
	 */
	private JButton submitButton;

	/**
	 * The button which clicked will cancel the create group request.
	 */
	private JButton cancelButton;

	/**
	 * The client instance which provide the service.
	 */
	private Client client;

	/**
	 * The user object which represent the logined user.
	 */
	private User user;

	/**
	 * The constructor which is to initialize the corresponding control
	 * 
	 * @param client
	 *            The clienk instance which provide the service.
	 * @param user
	 *            The user object which represent the logined user.
	 */
	public CreateGroupPanel(Client client, User user) {

		this.client = client;
		this.user = user;
		groupnameLabel = new JLabel("Group Name:");
		descriptionLabel = new JLabel("Description:");
		groupnameField = new JTextField(20);
		descriptionTextArea = new JTextArea();
		descriptionTextArea.setRows(3);
		descriptionTextArea.setColumns(20);
		descriptionTextArea
				.setToolTipText("Input group brief description here");
		descriptionTextArea.setEditable(true);
		descriptionTextArea.setLineWrap(true);
		JScrollPane jsDescription = new JScrollPane(descriptionTextArea,
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

		submitButton = new JButton("Submit");
		submitButton.addActionListener(new ListenerSubmitComfirm());

		cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(new ListenerCancelCreate());

		JPanel groupnamePanel = new JPanel();
		groupnamePanel.add(groupnameLabel);
		groupnamePanel.add(groupnameField);

		JPanel descriptionPanel = new JPanel();
		JPanel descLabelPanel = new JPanel();
		descLabelPanel.setLayout(new GridLayout(2, 1));
		descLabelPanel.add(descriptionLabel);
		descLabelPanel.add(new JLabel(""));
		descriptionPanel.add(descLabelPanel);
		descriptionPanel.add(jsDescription);

		JPanel operationPanel = new JPanel();
		operationPanel.add(submitButton);
		operationPanel.add(cancelButton);

		JPanel wholePanel = new JPanel();
		wholePanel.setLayout(new GridLayout(5, 1));
		wholePanel.add(new JLabel(""));
		wholePanel.add(groupnamePanel);
		wholePanel.add(descriptionPanel);
		wholePanel.add(new JLabel(""));
		wholePanel.add(operationPanel);

		frame = new JFrame("Create new group");
		frame.setContentPane(wholePanel);
		initFrame();
	}

	/**
	 * This method is to initialize the frame window.
	 */
	private void initFrame() {
		frame.setResizable(false);
		frame.setVisible(true);
		frame.setSize(WIDTH, HEIGHT);

		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = frame.getSize();
		if (frameSize.height > screenSize.height) {
			frameSize.height = screenSize.height;
		}
		if (frameSize.width > screenSize.width) {
			frameSize.width = screenSize.width;
		}
		frame.setLocation((screenSize.width - frameSize.width) / 2,
				(screenSize.height - frameSize.height) / 2);
	}

	/**
	 * This class is to deal with when user clicked in to submit the the create
	 * group request.
	 * 
	 * @author Joey George
	 * @version 1.0
	 * 
	 */
	class ListenerSubmitComfirm implements ActionListener {
		/**
		 * This method is to deal with the logic when a user clicked the create
		 * group button.
		 */
		@Override
		public void actionPerformed(ActionEvent event) {
			String groupname = groupnameField.getText().trim();
			String description = descriptionTextArea.getText().trim();

			boolean isSuccessful = checkParameter(groupname, description);
			if (isSuccessful) {
				IGroupService groupService = client.getGroupService();
				try {
					groupService.addGroup(user, groupname, description);
				} catch (RemoteException e) {
					e.printStackTrace();
				}

				String title = "Request new group successfulfy";
				String message = "You have successfully created a new Group!";
				frame.dispose();
				PromotDialog.successMessageBox(null, title, message);
			}

		}

		/**
		 * This method is to check the parameter when a user want to submit the
		 * create group request.
		 * 
		 * @param groupname
		 *            The group name which the user is want to create.
		 * @param description
		 *            The description which the user is want to create.
		 * @return Whether the parameter is all legal.
		 */
		private boolean checkParameter(String groupname, String description) {
			String reason = "";
			String title = "Parameter Error!";
			if (groupname.equals("")) {
				reason = "Group name is required!";
				PromotDialog.errorMessageBox(null, title, reason);
				return false;
			} else if (description.equals("")) {
				reason = "Group brief description is required!";
				PromotDialog.errorMessageBox(null, title, reason);
				return false;
			}
			return true;
		}
	}

	/**
	 * This class is to deal with when user clicked to cancel the the create
	 * group request.
	 * 
	 * @author Joey George
	 * @version 1.0
	 * 
	 */
	class ListenerCancelCreate implements ActionListener {
		/**
		 * This method is to deal with the logic when a user clicked cancel the create
		 * group button.
		 */
		public void actionPerformed(ActionEvent event) {
			frame.dispose();
			new WelcomePanel(client);
		}
	}
}
